package com.talkweb.securitycamera.common.base;

import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @version 1.0.1
 * @description:
 * @program: security-camera
 * @Author xiaozhijun
 * @Date 2021/1/26 9:42
 */
public abstract class BaseAbility extends FractionAbility {

    //Ability 返回code
    protected static final int PERMISSION_CODE = 0X000000;

    @Override
    protected void onStart(Intent intent) {
        System.out.println("BaseAbility onstart");
        super.onStart(intent);
        if (getSlice() != null) {
            super.setMainRoute(getSlice().getName());
        }
        initPermission(getNeedPermissions());
    }


    public abstract <T extends BaseAbilitySlice> Class<T> getSlice();

    ;

    public String[] getNeedPermissions() {
        return new String[]{};
    }

    ;

    /**
     * 初始化权限申请
     * @param permission
     */
    protected final void initPermission(String[] permission) {
        //动态权限
        ArrayList<String> permissions = checkPermission(permission);
        int size = permissions.size();
        if (size > 0) {
            String[] array = permissions.toArray(new String[size]);
            requestPermissionsFromUser(array, PERMISSION_CODE);
        } else {
            onReqPerMissionSuccess();

            //TODO ohos.permission.DISTRIBUTED_DATA系统不允许代码请求，无法区分用户禁止/系统禁止申请，所以默认先跳过
//            //没有需要申请的权限，检测是否存在被禁止的权限
//            if (isAllPermissionAllow(permission)) {
//                //不存在 权限OK
//                onReqPerMissionSuccess();
//            } else {
//                //存在 提示异常
//                onReqPerMissionFail();
//            }
        }
    }

    /**
     * 检测是否存在用户禁止的场景存在
     *
     * @param permission
     * @return
     */
    private boolean isAllPermissionAllow(String... permission) {
        boolean isAllow = true;
        int lenth = permission.length;
        for (int i = 0; i < lenth; i++) {
            if (verifySelfPermission(permission[i]) == -1) {
                isAllow = false;
                break;
            }
        }
        return isAllow;
    }

    /**
     * 检测需要申请的权限组
     * @param permission
     * @return
     */
    private ArrayList<String> checkPermission(String... permission) {
        int lenth = permission.length;
        ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i < lenth; i++) {
            if (verifySelfPermission(permission[i]) != IBundleManager.PERMISSION_GRANTED
                    && canRequestPermission(permission[i])) {

                result.add(permission[i]);
            }
        }
        return result;
    }

    @Override
    public final void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_CODE:
                // 匹配requestPermissions的requestCode
                boolean isSuccess = true;
                if (permissions != null && grantResults != null && grantResults.length == permissions.length) {
                    int size = permissions.length;
                    for (int i = 0; i < size; i++) {
                        System.out.println("onRequestPermissionsFromUserResult permissions " + permissions[i] + "--result " + grantResults[i]);
                        if (grantResults[i] != IBundleManager.PERMISSION_GRANTED) {
                            // 权限被拒绝
                            onReqPerMissionRefuse(permissions[i], grantResults[i]);
                            isSuccess = false;
                        }
                    }
                } else {
                    //异常常见
                    onReqPerMissionFail();
                    isSuccess = false;
                }
                if (isSuccess) {
                    // 权限申请成功
                    onReqPerMissionSuccess();
                }
                break;
        }
    }

    /**
     * 单个权限被拒绝
     * @param permission
     * @param grantResult
     */
    public void onReqPerMissionRefuse(String permission, int grantResult) {
    }

    /**
     * 请求权限成功
     */
    public void onReqPerMissionSuccess() {
    }

    /**
     * 请求权限异常
     */
    public void onReqPerMissionFail(){}
}
